package mapEditor.view
{
	import asset.mapEditor.assetClassListAsset;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mapEditor.events.MapEditorEvent;
	import mapEditor.model.EditorModel;

	public class AssetClassListView extends assetClassListAsset
	{
		private var _model:EditorModel;
		private var _xmlList:XML;
		private var _rowNumber:int=3;//每行个数
		private var _itemList:Array=[];
		
		public function AssetClassListView(model:EditorModel, list:XML)
		{
			_model=model;
			_xmlList=list;
			
			setView();
		}
		
		private function setView():void
		{
			if(assetPos.parent) assetPos.parent.removeChild(assetPos);
			txtClassName.text=_xmlList.@name;
			loadItemList();
		}
		
		private function loadItemList():void
		{
			if(!_xmlList) return;
			
			for(var i:int=0;i<_xmlList.item.length();i++)
			{
				var item:AssetClassItemView=new AssetClassItemView(_model, _xmlList.item[i]);
				item.doubleClickEnabled=true;
				item.addEventListener(MouseEvent.DOUBLE_CLICK, __onItemDoubleClick);
				if (i<_rowNumber)
				{
					item.x=4+i*(item.width+4);
					item.y=assetPos.y;
				} else {
					item.x=4+(i-int(i/_rowNumber)*_rowNumber)*(item.width+4);
					item.y=assetPos.y+int(i/_rowNumber)*(item.height+4);
				}
				addChild(item);
				_itemList.push(item);
			}
		}
		
		private function __onItemDoubleClick(evt:MouseEvent):void
		{
			dispatchEvent(new MapEditorEvent(MapEditorEvent.MAP_ASSET_DOUBLE_CLICK));
		}
		
		private function clearList():void
		{
			for each(var item:AssetClassItemView in _itemList)
			{
				item.removeEventListener(MouseEvent.DOUBLE_CLICK, __onItemDoubleClick);
				if(item.parent) item.parent.removeChild(item);
				item.dispose();
				item=null;
			}
		}
		
		public function dispose():void
		{
			clearList();
			_xmlList=null;
			_itemList=null;
			if(parent) parent.removeChild(this);
		}

		public function get itemList():Array
		{
			return _itemList;
		}

		public function set itemList(value:Array):void
		{
			_itemList = value;
		}
	}
}